package com.diandiji.demo.service.impl;

import com.diandiji.demo.dao.AccountbookMapper;
import com.diandiji.demo.entity.Accountbook;
import com.diandiji.demo.service.AccountbookService;
import com.diandiji.demo.vo.AccountbookListVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class AccountbookServiceImpl implements AccountbookService {

    @Resource
    private AccountbookMapper accountbookMapper;

    @Override
    public AccountbookListVO queryByMonth(String date) {
        AccountbookListVO resp = new AccountbookListVO();
        // 封装查询条件，查询指定月的所有数据(数据库中的日期数据为yyyy-MM-dd)
        List<Accountbook> list = accountbookMapper.queryByMonth(date);
        // 所有日期格式调整，只展示年月日返回
        for (Accountbook accountbook : list) {
            LocalDate localDate = accountbook.getDate()
                    .toInstant()
                    .atZone(ZoneId.systemDefault())
                    .toLocalDate();
            accountbook.setDate(Date.valueOf(localDate));
        }
        resp.setAccountbookList(list);
        // 设置收入
        List<Accountbook> collect = list.stream()
                .filter(accountbook -> accountbook.getType()
                        .equals("收入"))
                .toList();
        Double totalIncome = 0.0;
        if (!collect.isEmpty()) {
            for (Accountbook accountbook : collect) {
                totalIncome += accountbook.getMoney();
            }
        }
        resp.setTotalIncome(totalIncome);
        // 设置支出
        List<Accountbook> collect1 = list.stream()
                .filter(accountbook -> accountbook.getType()
                        .equals("支出"))
                .toList();
        Double totalExpenditure = 0.0;
        if (!collect1.isEmpty()) {
            for (Accountbook accountbook : collect1) {
                totalExpenditure += accountbook.getMoney();
            }
        }
        resp.setTotalExpenditure(totalExpenditure);

        return resp;
    }

    @Override
    public Accountbook add(Accountbook accountbook) {
        if (Objects.equals(accountbook.getType(), "1")) {
            accountbook.setType("收入");
        }
        // 确保日期字段只保留年月日
        if (accountbook.getDate() != null) {
            LocalDate localDate = accountbook.getDate()
                    .toInstant()
                    .atZone(ZoneId.systemDefault())
                    .toLocalDate(); // 修复点：正确转换为 LocalDate
            accountbook.setDate(Date.valueOf(localDate)); // 将 LocalDate 转换回 java.sql.Date
        }
        // 保存数据库
        accountbookMapper.insert(accountbook);
        return accountbook;
    }

    @Override
    public void deleteById(Integer id) {
        accountbookMapper.deleteById(id);
    }

    @Override
    public void update(Accountbook accountbook) {
        accountbookMapper.update(accountbook);
    }
}